package com.anjie.powerproject.repository;

import com.anjie.powerproject.entity.GraphDev;
import com.anjie.powerproject.pojo.CustomAtlasDevice;
import com.anjie.powerproject.pojo.CustomDevice;
import com.anjie.powerproject.strategy.pojo.device.BaseDevice;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface GraphDevRepository extends JpaRepository<GraphDev,Integer> {

    void deleteGraphDevsByDeviceId(Integer deviceId);

    void deleteGraphDevsByGraphId(Integer graphId);

    @Query(value = "select new com.anjie.powerproject.pojo.CustomAtlasDevice(d.deviceId,d.deviceName,d.picPath,d.highlightPicPath,g.xCoordinates,g.yCoordinates) from Device d,GraphDev g where d.deviceId = g.deviceId and g.graphId = ?1")
    List<CustomAtlasDevice> selectAtlasDeviceByGraphId(Integer graphId);

    @Query(value = "select new com.anjie.powerproject.pojo.CustomDevice(d.deviceId,d.deviceName) from Device d,GraphDev g where d.deviceId = g.deviceId and g.graphId = ?1")
    List<CustomDevice> selectCustomDevicesByGraphId(Integer graphId);

    List<GraphDev> findGraphDevsByGraphId(Integer graphId);

    List<GraphDev> findGraphDevsByDeviceIdAndGraphId(Integer deviceId,Integer graphId);

    @Transactional
    @Modifying
    @Query(value = "update GraphDev d set d.xCoordinates = ?1,d.yCoordinates = ?2,d.version=d.version+1 where d.deviceId = ?3 and d.graphId = ?4 and d.version = ?5")
    int updateDeviceCoordinates(float x,float y,Integer deviceId,Integer graphId, Integer version);

    @Query(value = "select new com.anjie.powerproject.strategy.pojo.device.BaseDevice(d.deviceId,d.deviceName,d.deviceType) from Device d,GraphDev g where d.deviceId = g.deviceId and g.graphId=?1 and d.isPredicted=?2")
    List<BaseDevice> selectDeviceByGraphIdInSummer(Integer graphId, Integer isPredicted);

    List<GraphDev> findGraphDevsByDeviceId(Integer deviceId);
}
